package com.mtz.service.impl;

import com.mtz.VO.QuesAnsVO;
import com.mtz.dateobject.Answer;
import com.mtz.dateobject.Question;
import com.mtz.form.QuesAnsForm;
import com.mtz.service.AnswerService;
import com.mtz.service.QuesAnsService;
import com.mtz.service.QuestionHistoryService;
import com.mtz.service.QuestionService;
import com.mtz.utils.MergeQuesAnsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 煜赜
 * @date 2018/12/3 0003 上午 11:08
 */
@Service
@Slf4j
public class QuesAnsServiceImpl implements QuesAnsService {

    @Autowired
    private QuestionHistoryService questionHistoryService;

    @Autowired
    private QuestionService questionService;

    @Autowired
    private AnswerService answerService;

    @Override
    public QuesAnsVO findQuesAnsById(Integer questionId) {
        Question question = questionService.findQuesById(questionId);
        Answer answer = answerService.findAnsById(question.getAnswerId());
        // 把问题、答案转换成问答形式
        return MergeQuesAnsUtil.merge(question, answer);
    }

    @Override
    public List<QuesAnsVO> findAll() {
        List<Question> questionList = questionService.findAll();

        List<QuesAnsVO> quesAnsVOList = new ArrayList<QuesAnsVO>();
        // 循环合并 Question,Answer ==>  QuesAnsVO（问题+答案）
        for (Question question : questionList) {
            // 查找问题的答案
            Answer answer = answerService.findAnsById(question.getAnswerId());
            QuesAnsVO quesAnsVO = MergeQuesAnsUtil.merge(question, answer);

            quesAnsVOList.add(quesAnsVO);
        }
        return quesAnsVOList;
    }

    @Override
    public List<QuesAnsVO> findAllByKnowledgeType(Integer knowledgeType) {
        List<Question> questionList = questionService.findQuesByKnowledgeType(knowledgeType);

        List<QuesAnsVO> quesAnsVOList = new ArrayList<QuesAnsVO>();
        // 循环合并 Question,Answer ==>  QuesAnsVO（问题+答案）
        for (Question question : questionList) {
            // 查找问题的答案
            Answer answer = answerService.findAnsById(question.getAnswerId());
            QuesAnsVO quesAnsVO = MergeQuesAnsUtil.merge(question, answer);

            quesAnsVOList.add(quesAnsVO);
        }
        return quesAnsVOList;
    }

    @Override
    public List<QuesAnsVO> findAllByKeyword(String keyword) {
        List<Question> questionList = questionService.findQuesByKeyword(keyword);

        List<QuesAnsVO> quesAnsVOList = new ArrayList<QuesAnsVO>();
        // 循环合并 Question,Answer ==>  QuesAnsVO（问题+答案）
        for (Question question : questionList) {
            // 查找问题的答案
            Answer answer = answerService.findAnsById(question.getAnswerId());
            QuesAnsVO quesAnsVO = MergeQuesAnsUtil.merge(question, answer);

            quesAnsVOList.add(quesAnsVO);
        }
        return quesAnsVOList;
    }

    @Transactional
    @Override
    public QuesAnsVO addQuesAns(QuesAnsForm quesAnsForm) {
        // 新增问题
        Question question = questionService.addQuestion(quesAnsForm.getKnowledgeType(),
                quesAnsForm.getQuestion(), quesAnsForm.getKeyword(), quesAnsForm.getIsDeleted());

        // 新增答案
        Answer answer = answerService.addAnswer(question.getId(), quesAnsForm.getAnswer(),
                quesAnsForm.getAuthor(), quesAnsForm.getAngle());

        // 问题关联答案
        questionService.setQuesAnswerId(question.getId(), answer.getId());

        QuesAnsVO quesAnsVO = MergeQuesAnsUtil.merge(question, answer);

        // 插入历史记录
        quesAnsForm.setId(question.getId());
        questionHistoryService.addHistory(quesAnsForm);
        return quesAnsVO;
    }

    @Override
    public void editQuesAns(QuesAnsForm quesAnsForm) {
        // 修改问题
        Question question = questionService.editQuestion(quesAnsForm.getId(),
                quesAnsForm.getQuestion(), quesAnsForm.getKeyword());

        // 修改答案
        answerService.editAnswer(question.getAnswerId(), quesAnsForm.getAnswer(),
                quesAnsForm.getAuthor(), quesAnsForm.getAngle());

        // 插入历史记录
        questionHistoryService.addHistory(quesAnsForm);
    }

    @Override
    public void deleteQuesAns(Integer questionId) {
//        Question question = questionService.findQuesById(questionId);
        List<Answer> answerList = answerService.findAnsByQuestionId(questionId);
        for (Answer answer : answerList) { //删除所有答案
            answerService.trueDelete(answer.getId());
        }
        // 删除问题
        questionService.trueDelete(questionId);
    }
}
